<template>
  <el-dialog ref="dislogRef" :visible.sync="dialogVisible" lock-scroll width="60%" height="50%">
    <div slot="title" class="header_title">
      <span class="title_span">选择商品</span>
      <div class="title_search">
        <div>
          <el-input
            @change="onQuery"
            v-model="queryData.keyword"
            placeholder="请输入商品名称\条码\编码\规格\拼音码"
          ></el-input>
          <el-button type="primary" @click="onQuery">搜索</el-button>
        </div>
        <div>
          <i @click="toDetail()" class="set_up el-icon-full-screen"></i>
        </div>
      </div>
    </div>
    <div class="content_div">
      <div class="info_table">
        <div class="div_tree">
          <div class="tree_div1">
            <p>商品类别</p>
          </div>
          <div class="tree_input">
            <p>
              <input type="text" @change="onSelectTree()" placeholder="查找" v-model="GoodsTypeName" />
              <span @click="onSelectTree()">
                <i class="el-icon-search"></i>
              </span>
            </p>
          </div>
          <div class="tree_structure">
            <el-tree
              :data="treeData"
              node-key="label"
              :props="defaultProps"
              highlight-current
              default-expand-all
              :expand-on-click-node="false"
              @node-click="handleNodeClick"
            ></el-tree>
          </div>
        </div>
        <div class="div_table">
          <div class="table_content">
            <el-table
              :data="tableData.list"
              height="100%"
              @row-dblclick="handleDblclick"
              @row-click="onClickRow"
            >
              <el-table-column :resizable="false" label="序号" type="index" width="50"></el-table-column>
              <el-table-column
                :show-overflow-tooltip="true"
                v-for="(item, i) in newColumnData"
                :key="i"
                :prop="item.props"
                :label="item.morenName"
                min-width="150px"
              ></el-table-column>
            </el-table>
          </div>
          <div class="table_pagination">
            <p class="total_div">共{{ total }}条记录</p>
            <div class="pagination_div">
              <el-pagination
                background
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
                :current-page="currentPage"
                :page-sizes="[20, 50, 100, 200]"
                :page-size="pagesize"
                layout="jumper, prev, pager,next, sizes"
                :total="total"
              ></el-pagination>
            </div>
          </div>
        </div>
      </div>
      <div class="info_button">
        <el-button type="primary" @click="onSelectRow">选择</el-button>
        <el-button @click="onCancelRow">取消</el-button>
      </div>
    </div>
  </el-dialog>
</template>

<script>
import { mapGetters } from 'vuex'
import { getAction } from '@/api/manage'
export default {
  name: 'BasicInformation',
  computed: {
    ...mapGetters(['name']),
  },
  created () {
    this.getData()
  },
  data () {
    return {
      GoodsTypeName: '',
      queryData: {
        keyword: '',
        goodsTypeId: '',
      },
      pagesize: 20, //每页的数据条数
      currentPage: 1, //默认开始页面
      tableData: {},
      newColumnData: [
        {
          morenName: '商品编码',
          props: 'goodsNumber',
        },
        {
          morenName: '条形码',
          props: 'barCode',
        },
        {
          morenName: '商品名称',
          props: 'goodsName',
        },
        {
          morenName: '基本单位',
          props: 'baseUnit',
        },
        {
          morenName: '规格',
          props: 'specs',
        },
        {
          morenName: '库存数量',
          props: 'number',
        },
        {
          morenName: '可用数量',
          props: 'useNumber',
        },
        {
          morenName: '辅助单位',
          props: 'helpUnit',
        },
        {
          morenName: '辅助单位规格',
          props: 'specss',
        },
        {
          morenName: '商品类别',
          props: 'xghGoodsTypeName',
        },
        {
          morenName: '零售价',
          props: 'retailPrice',
        },
        {
          morenName: '预设售价一',
          props: 'presetPriceOne',
        },
        {
          morenName: '预设售价二',
          props: 'presetPriceTwo',
        },
        {
          morenName: '预设进价',
          props: 'presetBidPrice',
        },
        {
          morenName: '商品性质',
          props: 'goodsNatureName',
        },
        {
          morenName: '预设折扣一',
          props: 'discountRatioOne',
        },
        {
          morenName: '预设折扣二',
          props: 'discountRatioTwo',
        },
        {
          morenName: '拼音码',
          props: 'pinyinCode',
        },
        {
          morenName: '备注',
          props: 'goodsRemark',
        },
      ],
      treeData: [],
      defaultProps: {
        children: 'children',
        label: 'goodsTypeName',
      },
      selectRow: {}, //当前选择的数据
      dialogVisible: false, // 新增弹框
      total: 0,
      selectId: ''
    }
  },
  methods: {
    // 单元格被双击时
    handleDblclick (row) {
      this.dialogVisible = false
      this.$emit('handleDblclick', row)
    },
    // 获取数据
    getData () {
      Object.assign(this.$data, this.$options.data())
      this.getTreeData()
    },
    // 获取类别数据
    getTreeData () {
      let data = {
        GoodsTypeName: this.GoodsTypeName,
      }
      getAction('/goodType/treeList', data).then((res) => {
        if (res.code === 200) {
          this.treeData = res.result
        }
      })
    },
    // 获取表格数据
    async getTableData (id) {
      this.selectId = id
      let data = {
        ...this.queryData,
        pageSize: this.pagesize,
        pageNo: this.currentPage,
        storehouseId: this.selectId,
      }
      const res = await getAction('/xgh/storehouseGoods/selectGoods', data)
      if (res.code === 200) {
        this.tableData = res.result
        this.tableData.list.forEach((item) => {
          item.disabled_status = item.disabled_status == 0 ? true : false
        })
        this.total = res.result.total
      }
    },
    // 搜索
    onQuery () {
      this.getTableData()
    },
    // 全屏显示
    toDetail () {
      this.$router.push({
        name: 'commodityInfo',
      })
    },
    // 搜索树形结构数据
    onSelectTree () {
      this.getTreeData()
    },
    // 触发树形当前节点
    handleNodeClick (data) {
      if (data.superiorId === 0) {
        this.queryData.goodsTypeId = ''
      } else {
        this.queryData.goodsTypeId = data.id
      }
      this.nowLabel = data.goodsTypeName
      this.getTableData()
    },
    // 选中当前行
    onClickRow (row) {
      this.selectRow = row
    },
    // 选中后传递给父组件
    onSelectRow () {
      if (this.selectRow == {}) {
        this.$elMessage.warning('请选择一条信息')
        return
      }
      this.$emit('handleDblclick', this.selectRow)
      this.dialogVisible = false
    },
    // 取消
    onCancelRow () {
      this.$emit('onCancelCommodity', this.selectRow)
      this.dialogVisible = false
    },
    // 获取每页几条数据
    handleSizeChange (val) {
      this.pagesize = val
      this.getTableData()
    },
    // 获取当前第几页
    handleCurrentChange (val) {
      this.currentPage = val
      this.getTableData()
    },
  },
}
</script>

<style lang="scss" scoped>
::v-deep .el-dialog {
  width: 100%;
  .el-dialog__body {
    padding: 0;
  }
  .el-dialog__header {
    width: 100%;
    height: 0.937559rem;
    box-sizing: border-box;
    padding: 0 0.187512rem 0 0.281268rem;
    border-bottom: 1px solid #eeeeee;
    display: flex;
    align-items: center;
    justify-content: space-between;
    .el-dialog__headerbtn {
      position: static;
      .el-icon-close {
        color: #999;
        font-size: 0.48753rem;
      }
    }
  }
  .header_title {
    width: 100%;
    display: flex;
    align-items: center;
    .title_span {
      white-space: nowrap;
      font-size: 0.300019rem;
      color: #666;
      font-weight: 600;
    }
    .title_search {
      width: 100%;
      display: flex;
      align-items: center;
      justify-content: space-between;
      box-sizing: border-box;
      padding: 0 0.468779rem 0 1.875117rem;
      > div {
        display: flex;
        align-items: center;
        .el-input {
          width: 4.725295rem;
          height: 0.562535rem;
          margin-right: 0.112507rem;
          .el-input__inner {
            height: 0.562535rem;
            border-color: #dddddd;
            box-sizing: border-box;
            padding: 0 0.150009rem;
            background: #fff;
            font-size: 0.225014rem;
            color: #666;
            &::placeholder {
              font-size: 0.225014rem;
              color: #c1c1c1;
            }
          }
        }
        .el-button {
          width: 1.12507rem;
          height: 0.562535rem;
          text-align: center;
          box-sizing: border-box;
          display: flex;
          align-items: center;
          justify-content: center;
          font-size: 0.225014rem;
          border-radius: 0.093756rem;
          border: none;
          margin-right: 0.112507rem;
          span {
            color: #fff;
            font-size: 0.225014rem;
            font-weight: normal;
          }
        }
      }
    }
    .set_up {
      font-size: 0.375023rem;
      color: rgb(153, 153, 153);
    }
  }
}
.content_div {
  width: 100%;
  height: 60vh;
  background: #fff;
  box-sizing: border-box;
  padding: 0.187512rem 0.281268rem;
  .info_table {
    width: 100%;
    height: calc(100% - 0.750047rem);
    display: flex;
    .div_tree {
      width: 2.812676rem;
      height: calc(100% - 1.12507rem);
      box-sizing: border-box;
      padding: 0.131258rem 0;
      border: 0.018751rem solid #e3e3e3;
      transition: all 0.3s;
      .tree_div1 {
        width: 100%;
        height: 0.337521rem;
        box-sizing: border-box;
        padding: 0 0.168761rem;
        display: flex;
        align-items: center;
        justify-content: space-between;
        p {
          color: #666666;
          font-size: 0.225014rem;
        }
      }
      .tree_input {
        width: 100%;
        height: 0.562535rem;
        margin-top: 0.131258rem;
        box-sizing: border-box;
        padding: 0 0.168761rem;
        p {
          width: 100%;
          height: 0.562535rem;
          margin: 0;
          box-sizing: border-box;
          border: 1px solid #dddddd;
          display: flex;
          align-items: center;
          position: relative;
          input {
            width: 100%;
            box-sizing: border-box;
            padding: 0 0.600038rem 0 0.093756rem;
            height: 0.525033rem;
            border: none;
            font-size: 0.225014rem;
            &:focus {
              outline: none;
            }
          }
          span {
            position: absolute;
            right: 0;
            display: flex;
            align-items: center;
            justify-content: center;
            width: 0.562535rem;
            height: 0.525033rem;
            background: #fff;
            .el-icon-search {
              font-size: 0.31877rem;
              color: rgb(211, 211, 211);
            }
          }
        }
      }
      .tree_structure {
        width: 100%;
        height: calc(100% - 0.337521rem - 0.131258rem - 0.131258rem - 0.562535rem);
        box-sizing: border-box;
        padding: 0 0.168761rem;
        margin-top: 0.131258rem;
        overflow: auto;
        ::v-deep .el-tree {
          width: 100%;
          .el-tree-node__children {
            .el-tree-node__label {
              color: #666;
              font-weight: normal;
            }
          }
          .el-tree-node__label {
            font-size: 0.225014rem;
            font-weight: 600;
          }
          .is-current {
            .el-tree-node__content {
              background-color: #e6f7ff;
            }
          }
          .el-tree-node__content > .el-tree-node__expand-icon {
            padding-left: 0;
          }
        }
      }
    }
    .div_table {
      width: calc(100% - 3.000188rem);
      height: 100%;
      margin-left: 0.187512rem;
      display: flex;
      flex-direction: column;
      justify-content: space-between;
      transition: all 0.3s;
      .table_content {
        height: calc(100% - 1.12507rem);
        ::v-deep .el-table {
          border: 1px solid #e3e3e3;
          box-sizing: border-box;
          th,
          td {
            padding: 0;
            color: #666;
            box-sizing: border-box;
            border-right: 1px solid #e3e3e3;
            .cell {
              height: 0.562535rem;
              display: flex;
              align-items: center;
              font-size: 0.225014rem;
            }
            &:first-child {
              .cell {
                justify-content: center;
              }
            }

            &:last-child {
              border-right: none;
            }
          }
          th {
            .cell {
              background: #ededed;
              justify-content: center;
            }
          }
          .el-table__body tr:hover > td {
            background: #e3f6ff;
          }
          .el-table__footer-wrapper {
            td {
              background: #f4f4f4;
              border: none;
            }
          }
          //滚动条的宽度
          ::-webkit-scrollbar {
            width: 10px;
            height: 10px;
            background-color: #e5e5e5;
          }
          //滚动条的滑块
          ::-webkit-scrollbar-thumb {
            background-color: #c9c9c9;
          }
        }
      }
      .table_pagination {
        width: 100%;
        height: 1.12507rem;
        box-sizing: border-box;
        padding: 0.168761rem 0;
        display: flex;
        .total_div {
          line-height: 0.787549rem;
          font-size: 0.225014rem;
          color: #666;
          margin: 0;
        }
        .pagination_div {
          flex: 1;
          display: flex;
          align-items: center;
          justify-content: flex-end;
          ::v-deep .el-pagination {
            padding: 0;
            height: 0.525033rem;
            .el-pagination__sizes {
              margin: 0;
              .el-input {
                margin-right: 0;
              }
            }
          }
        }
      }
    }
  }
  .info_button {
    width: 100%;
    height: 0.750047rem;
    display: flex;
    justify-content: flex-end;
    ::v-deep .el-button {
      width: 1.12507rem;
      height: 0.562535rem;
      display: flex;
      justify-content: center;
      align-items: center;
      border-radius: 0.093756rem;
    }
  }
}
</style>